package com.jfire.socket.socketserver.bus;

import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.jfire.socket.socketserver.handler.MessageHandler;
import com.jfire.socket.socketserver.interceptor.MessageInterceptor;

@Resource
public class CenterSwitch
{
    
    @Resource
    private MessageHandlerCenter handlerCenter;
    private Logger               logger = LogManager.getLogger();
    private Thread               requestThread;
    
    public void init()
    {
        logger.debug("加载请求报文处理模块：{}", handlerCenter.getClass().getName());
        logger.debug("加载的消息处理器有：");
        for (MessageHandler handler : handlerCenter.getHandlerList())
        {
            logger.debug("{}", handler.getClass().getName());
        }
        logger.debug("加载的消息拦截器有：");
        for (MessageInterceptor interceptor : handlerCenter.getInterceptorList())
        {
            logger.debug("{}", interceptor.getClass().getName());
        }
        handlerCenter.init();
        requestThread = new Thread(handlerCenter, "任务处理中心线程");
        requestThread.start();
    }
    
    public void stop() throws InterruptedException
    {
        requestThread.interrupt();
        handlerCenter.stop();
    }
}
